<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Image;
use think\Db;

class Mater extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    /**
    *图片上传方法
    *Service/upload
    *2018.03.30
    *BY HF
    */
    public function upload(){
    	$file=$this->request->file('Filedata');
    	$path=ROOT_PATH . 'public' . DS . 'uploads' . DS .$this->request->param('folder');
    	if($file){
	        $info = $file->move($path);
	        if($info){
	            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
	            // echo $info->getSaveName();
	            // 输出 42a79759f284b767dfcb2a0197904287.jpg
	            // echo $info->getFilename(); 
	            $image = Image::open($path.'/'.$info->getSaveName());
	            // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
	            // $image->thumb(400,400)->save($path . DS . date('Ymd') . DS .'md_'.$info->getFilename());
	            // $image->thumb(100,100,Image::THUMB_CENTER)->save($path . DS . date('Ymd') . DS .'sm_'.$info->getFilename());
	            $imgpath=$info->getSaveName();

	            $html="
	            <div class='appendimg' id='appendimg'>
                     <img src='/uploads/".$this->request->param('folder').'/'.$imgpath."'>  
                 </div>
                 <div class='del' data-imgpath='/uploads/".$this->request->param('folder').'/'.$info->getSaveName()."'>
                     <i class='Hui-iconfont'>&#xe609;</i>
                 </div>
                 <input type='hidden' name='img' value='{$imgpath}'>
	            ";
	            return $html;
	        }else{
	            // 上传失败获取错误信息
	            return $file->getError();
	        }
	    }
    } 
    /**
    *删除图片
    */
    public function del_upload_img(){
    	$imgpath=$this->request->param('imgpath');
    	///uploads/mater/20180330\23b76354e799d556e2873d77394b29e6.jpg
    	if(unlink("./{$imgpath}")){
    		echo 1;
    	}else{
    		echo 0;
    	}
    }

    public function add(){
    	if($this->request->isAjax()){
    		if($this->request->param('id')){
    			//编辑	
    			$param=$this->request->param();

    			echo'<pre>'; 
    			print_r($param); 
    			echo'</pre>'; 

    		}else{
    			//添加
		    	$param=$this->request->except('id');
		    	$name=$param['name'];
		    	$count=Db::name('mater')->where('name',$name)->count();
		    	if($count){
		    	    return  ajax_return_adv_error('数据已存在，不能重复添加');
		    	}
		    	$ret = Db::name('mater')->insert($param);
		    	if($ret){
					return ajax_return_adv('添加成功');		    	        
		    	}
		    	return ajax_return_adv_error('添加失败');
    		}

    	}else{
            $mater_class=Db::name('mater_class')->where(['style'=>0])->order('id desc')->select();
            $this->view->assign('list',$mater_class);
    		return $this->view->fetch('edit');
    	}
    }

    public function edit(){

        if($this->request->isAjax()){

            $ret=Db::name('mater')->update($this->request->post());
            if($ret){
                return ajax_return_adv('操作成功');                     
            }
            return ajax_return_adv_error('操作失败');
            
                    
        }else{
            $id=$this->request->param('id');
            $vo=Db::name('mater')->find($id);
            $mater_class=Db::name('mater_class')->where(['style'=>$vo['style']])->order('id desc')->select();
            $this->view->assign('val',$vo);
            $this->view->assign('list',$mater_class);
        }
        return $this->view->fetch();
    }

    public function index(){
        $map=[];
        if($this->request->param('name')){
            $map['m.name']=["like", "%" . $this->request->param("name") . "%"];
        }
        $list=Db::table('tp_mater')->alias('m')
                                    ->join('mater_class mc','m.mater_class_id=mc.id and m.style=mc.style','LEFT')
                                    ->field('m.*,mc.name class_name')
                                    ->where($map)
                                    ->order('id desc')
                                    ->paginate(10);

        // echo Db::getLastSql();

        //SELECT `m`.*,mc.name class_name FROM `tp_mater` `m` LEFT JOIN `tp_mater_class` `mc` ON `m`.`mater_class_id`=mc.id and m.style=mc.style WHERE  `m`.`name` = '材料' ORDER BY id desc LIMIT 0,10
        
        $this->view->assign('list',$list);
        $page = $list->render();
        // 模板变量赋值
        $this->view->assign('count',$list->total());
        $this->view->assign('page', $page);  
        return $this->view->fetch();

    }

    public function get_mater_class(){
        $style=$this->request->param('style');
        $mater_class=Db::name('mater_class')->where(['style'=>$style])->order('id desc')->select();
        $this->view->assign('list',$mater_class);
        return $this->view->fetch();
    }


    public function manger(){
        if($this->request->param('id')){
            $res=Db::name('mater_class')->find($this->request->param('id'));
            $this->view->assign('vo',$res);
        }
        $arr=DB::name('mater_class')->order('id desc')->paginate(8);
        $page = $arr->render();
        // 模板变量赋值
        $this->view->assign('count',$arr->total());
        $this->view->assign('page', $page);        
        $this->view->assign('list',$arr);
        
        return $this->view->fetch();
    }

    public function edit_class(){
        if($this->request->post('id')){
           //修改 
            if(Db::name('mater_class')->update($this->request->post())){
                return ajax_return_adv('操作成功'); 
            }else{
                return ajax_return_adv_error('操作失败,请及时联系开发人员'); 
            }
        }else{
            //提交
            $post=$this->request->except('id');
            $res=Db::name('mater_class')->insert($post);                    
            if($res){
                return ajax_return_adv('操作成功'); 
            }else{
                return ajax_return_adv_error('操作失败,请及时联系开发人员'); 
            }
        }
    }

    public function deleteForever(){
        $where=[
            'id'=>$this->request->param('id')
        ];

        $count=Db::name('mater')->where('mater_class_id',$this->request->param('id'))->count();


        if($count){
             return ajax_return_adv_error('该分类下有公司存在，不能删除');
        }
        if (false === Db::name('mater_class')->where($where)->delete()) {
            return ajax_return_adv_error($model->getError());
        }

        return ajax_return_adv("删除成功");
    }
    public function delete_mater(){
       $where=[
           'id'=>$this->request->param('id')
       ];
       if (false === Db::name('mater')->where($where)->delete()) {
           return ajax_return_adv_error($model->getError());
       }

       return ajax_return_adv("删除成功"); 
    }
}
